<?php
	// Ajaxanrop till denna fil,
	
	/*
	 * Funktioner:
	 *  1. ready - Flagga att en spelare �r redo
	 *  2. checkIfPlayersReady - Kolla om alla spelare �r redo
	 *  3. 
	 */
	
	include('Misc/DatabaseConnection.php');
	include('Misc/Settings.php');

	$db = new DatabaseConnection(Settings::DbSettings());
	$db->Connect();	
	
	// H�rdkodat
	$userId = 5;
	$gameId = 1;
	

	if(isset($_GET['func']) && $_GET['func'] == 'ready')
	{
		$ready = 1;
		
		
		    if($stmt = $db->getPrepareStatement("UPDATE PlayerTurns
		     									 SET Ready=?
		     									 WHERE UserID=? AND GameID=?"))
    		{
    			$stmt->bind_param("iii", $ready, $userId, $gameId);
    			if($stmt->execute())
    			{
    				$stmt->close();
    				return true;
    			}
    		}
    		return false;
		
		
		//TODO S�tt in en flagga i databasen f�r denna spelare, att han �r redo
		$_GET['playerId'];
	}
	
	if(isset($_GET['func']) && $_GET['func'] == 'checkIfPlayersReady')
	{
		// h�rdkodat
		$gameId = 6;
		
		$objArray = null;
		
		if($stmt = $db->getPrepareStatement("SELECT UserID, Ready 
											 FROM PlayerTurns 
											 WHERE GameID=?"))
		{
			$stmt->bind_param("i", $gameId);
			$stmt->execute();
			$stmt->bind_result($id,$readyStatus);
			while($stmt->fetch())
			{
				$obj = array("userId" => $id, "readyStatus" => $readyStatus); 
				$objArray[] = $obj; 
			}
			$stmt->close();
			
			foreach($objArray as $user)
			{
				if($user["readyStatus"] == false)
				{
					return false;
				}
			}
			return true;
		}	
		
			//TODO Kollar mot databasen & den specfika matchsession/tabellen vilka spelare som �r redo 
			//TODO returnera true/false
	}
	public function setReadyStatusToFalse($gameId)
	{
		if($stmt = $this->db->getPrepareStatement("UPDATE PlayerTurns 
											 SET Ready = 0 
											 WHERE GameId=?"))
		{
			$stmt->bind_param("i", $gameId);
			$stmt->execute();
			$stmt->close();
		}
	}
	
	$db->Close();
	// Vi ska kolla n�r alla gjort sina personliga drag
	// Och trigga det stora draget/turnen

	/*
	 * L�gga in en flagga att personen �r redo
	 * Kolla s� alla andra �r redo
	 * �r alla redo s� k�r maindraget 
	 */
	
	/* Saker som borde fixas
	 * Databas
	 * Registrering
	 *  
	 *  */
	
